package org.shop.controller.liveManager;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.util.StringUtil;
import org.shop.entity.ClassInfo;
import org.shop.service.ClassInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LiveManagerClassInfoController {

	private static Logger logger = LogManager.getLogger(LiveManagerClassInfoController.class);
	
	@Autowired
	private ClassInfoService classInfoService;
	
	/**
	 * 获取论坛分类列表
	 * @param req
	 * @return
	 */
	@RequestMapping("liveManager/getClassInfoList")
	@ResponseBody
	public Map<String,Object> getClassInfoList(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
	 	String currentPage = req.getParameter("offset");
        String pageSize = req.getParameter("limit");
        String className = req.getParameter("className");
		try{
			Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startPage", Integer.parseInt(currentPage));
            paramMap.put("pageSize", Integer.parseInt(pageSize));
            paramMap.put("className", className);
            map = classInfoService.getClassInfoList(paramMap);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}
	
	
	/**
	 * 添加论坛分类
	 * @param req
	 * @return
	 */
	@RequestMapping("liveManager/addClassInfo")
	@ResponseBody
	public int addClassInfo(HttpServletRequest req) {
		int num = 0;
		Map<String,Object> paramMap = new HashMap<String,Object>();
		String className = req.getParameter("className");
		String classIntro = req.getParameter("classIntro");
		try {
			paramMap.put("className", className);
			ClassInfo classInfo = classInfoService.getClassInfo(paramMap);
			if(classInfo!=null&&!"".equals(classInfo)) {
				num = -1;
			}else {
				ClassInfo ci = new ClassInfo();
				ci.setClassIntro(classIntro);
				ci.setClassName(className);
				num = classInfoService.insertSelective(ci );
			}
		}catch(Exception e) {
			num = 0;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num;
	}
	
	
	/**
	 * 修改分类名称
	 * @param req
	 * @return
	 */
	@RequestMapping("liveManager/updateClassInfo")
	@ResponseBody
	public int updateClassInfo(HttpServletRequest req) {
		int num = 0;
		Long classId = Long.parseLong(req.getParameter("classId"));
		String className = req.getParameter("className");
		try {
			ClassInfo classInfo = new ClassInfo();
			classInfo.setClassId(classId);
			classInfo.setClassName(className);
			num = classInfoService.updateByPrimaryKeySelective(classInfo );
		}catch(Exception e) {
			num = 0;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num;
	}
	
	
}
